﻿//***************************************************
//
//       File Name:    UIFlowTexture.cs
//
//       Author:   王文军    Email:  546527816@qq.com
//
//
//***************************************************

using UnityEngine;
using System.Collections;

/*-------------------------------------------------------------------
Copyright 2015 Minty Game LTD. All Rights Reserved.
Maintained by  blue 
-------------------------------------------------------------------
blue                2016-01-26 14:45:52
            带流光的UITexture
*/

[ExecuteInEditMode]
[RequireComponent(typeof(UITexture))]
public class UIFlowTexture : MonoBehaviour {

    //流光纹理的宽度
    [SerializeField]
    private float FlowWidth = 0.2f;

    //流光颜色
    [SerializeField]
    private Texture FlowTexture = null;

    //流光速度
    [SerializeField]
    private float FlowTime = 0.5f;

    //流光播放完之后的延迟时间
    [SerializeField]
    private float DelayTime = 1.0f;

    //流光纹理的斜度
    [SerializeField]
    private float Angle = -0.1f;

    //流光颜色
    [SerializeField]
    private Color FlowColor = Color.white;

    //是否开启流光
    [SerializeField]
    private bool Flow = true;

    //被流光的UITexture
    private UITexture mUITexture;

    void Awake()
    {
        mUITexture = gameObject.GetComponent<UITexture>();
    }

    // Use this for initialization
    void Start () 
    {
        mUITexture.material = Resources.Load("UITexture_Flow")as Material;
            RefreshMaterialProperty();
    }
    
    void SetFlow(bool flag)
    {
        if(Flow!=flag)
        {
            Flow = flag;
            RefreshMaterialProperty();
        }
    }

    [ContextMenu("Refresh Material Property")]
    public void RefreshMaterialProperty()
    {
        var mat = mUITexture.material;
        if(null==mat)
        {
            return;
        }

        if(null!=FlowTexture)
        {
            mat.SetTexture("_FlashTex", FlowTexture);
        }
        
        mat.SetFloat("_Width", FlowWidth);
        mat.SetFloat("_Speed", 1.0f/FlowTime);
        mat.SetFloat("_Angle", Angle);
        if (Flow)
        {
            mat.SetColor("_FlashColor", FlowColor); 
        }
        else
        {
            mat.SetColor("_FlashColor", new Color(0, 0, 0, 0));
        }
        if (null != mat)
        {
            mat.SetFloat("_TotalTime", FlowTime+DelayTime);
        }
        mUITexture.SetDirty();
    }


}